from django.http import HttpResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.template import loader

from blogs.models import Blog, Active, Comment
from user.models import User, Friend


def message(request):
    dic = {}
    if request.method == 'GET':
        return render(request,'blogs/message.html')
    elif request.method == 'POST':
        user_id = request.COOKIES.get('user_id')
        title = request.POST.get('title')
        intro = request.POST.get('intro')
        content = request.POST.get('content')
        print(user_id,title,intro)
        print(content)
        try:
            Blog.objects.create(user_id=user_id,title=title,intro=intro,content=content)
            user = User.objects.get(id=user_id)
            User.objects.filter(id=user_id).update(count=user.count+1)
            dic['mes'] = '发布成功'
            return render(request,'blogs/message.html',dic)
        except:
            dic['mes'] = '发生错误 请联系作者 请先登录'
            return render(request,'blogs/message.html',dic)


def detail(request,num):
    dic = {}
    blog = Blog.objects.get(id=num)
    user = User.objects.get(id=blog.user_id)
    user_id = request.COOKIES.get('user_id')

# 获取所有评论

    comments = Comment.objects.filter(blog_id=num).order_by('-id')
    comment_list = []
    for i in comments:
        user_comment = User.objects.get(id=i.user_id)
        comment_list.append(user_comment)
    dic['comments'] = comments
    dic['comment_list']=comment_list
# 判断是否点赞
    try:
        active = Active.objects.get(user_id=user.id,blog_id=blog.id)
        if active.is_active:
            dic['like'] = '点赞'
        else:
            dic['like'] = ''
    except:
        Active.objects.create(user_id=user.id,blog_id=blog.id)
        # dic['like'] = '点赞'
# 判断好友是否关注
    try:
        Friend.objects.get(user_id=user_id,other_id=user.id)
        dic['detail'] = ''
    except:
        dic['detail'] = '关注'

    dic['blog'] = blog
    dic['user'] = user
    return render(request,'blogs/detail.html',dic)


def friend(request):
    dic = {}
    #好友
    user_id = request.COOKIES.get('user_id')
    friends = Friend.objects.filter(user_id=user_id).values('other_id')
    user_friend = []
    for i in friends:
        print(i)
        user = User.objects.get(id=i['other_id'])
        user_friend.append(user)
    dic['friends'] = user_friend
    # 热门推荐
    users = User.objects.all().order_by('-count')[0:4]

    dic['users'] = users
    # 好友文章
    blog_list = []
    for item in user_friend:
        blogs = Blog.objects.filter(user_id=item.id).order_by('-id')
        user = User.objects.get(id=item.id)

        blog_list.append(blogs)

    dic['friends_blogs'] = blog_list
    return render(request,'blogs/friend.html',dic)


def like(request,num):
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)

    blog = Blog.objects.get(id=num)
    try:
        active = Active.objects.get(user_id=user.id, blog_id=blog.id)
        if active.is_active:
            blog.like += 1
            active.is_active = False
        else:
            blog.like -= 1
            active.is_active = True
        blog.save()
        active.save()
    except:
        Active.objects.create(user_id=user.id, blog_id=blog.id)


    return redirect('/')


def detail_like(request,num):
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)

    blog = Blog.objects.get(id=num)
    try:
        active = Active.objects.get(user_id=user.id, blog_id=blog.id)
        if active.is_active:
            blog.like += 1
            active.is_active = False
        else:
            blog.like -= 1
            active.is_active = True
        blog.save()
        active.save()
    except:
        Active.objects.create(user_id=user.id, blog_id=blog.id)

    return redirect(f'/blogs/{num}')


def all_forum(request,num):
    blogs = Blog.objects.filter(user_id=num).order_by('-id')

    return render(request,'index.html',{'blogs':blogs})


def like_friend(request,num):
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)

    blog = Blog.objects.get(id=num)
    try:
        active = Active.objects.get(user_id=user.id, blog_id=blog.id)
        if active.is_active:
            blog.like += 1
            active.is_active = False
        else:
            blog.like -= 1
            active.is_active = True
        blog.save()
        active.save()
    except:
        Active.objects.create(user_id=user.id, blog_id=blog.id)

    return redirect(f'/blogs/friend')


def user_blogs(request):
    dic = {}
    if request.method == 'GET':
        username = request.GET.get('username')
        user = User.objects.get(username=username)

        blogs = Blog.objects.filter(user_id=user.id)

        dic['friends_1'] = blogs

    return render(request,'blogs/friend.html',dic)


def comment(request,num):# 博客编号
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)

    content = request.POST.get('content')
    Comment.objects.create(user_id=user.id,content=content,blog_id=num,username=user.username)

    return redirect(f'/blogs/{num}')


def myblogs(request):
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)

    blogs = Blog.objects.filter(user_id=user.id,is_del=True).order_by('-id')

    return render(request,'blogs/ohter.html',locals())


def del_blog(request,num):
    blog = Blog.objects.get(id=num)
    blog.is_del = False
    blog.save()
    return redirect('/blogs/myblogs')